function setTab(defaultIndex)
{
    var self = plus.webview.currentWebview();
    var tabList = document.querySelectorAll(".mui-tab-item");
    for(var i = 0; i < tabList.length; i++){
        var href = tabList[i].getAttribute("href");
        var sub = plus.webview.create(href, href, {top: "45px", bottom: "51px"});
        if(i != defaultIndex){
            sub.hide();
        }
        else{
            tabList[i].classList.add("mui-active");
            tabList[i].classList.add("has-show");
        }
        self.append(sub);
    }
}

function doTap(e)
{
    var href = this.getAttribute("href");
    var hasActive = this.classList.contains("mui-active");
    var hasShow = this.classList.contains("has-show");
    /*点了当前的*/
    if(hasActive){
        return;
    }
    if(hasShow || mui.os.ios){
        plus.webview.show(href);
    }
    else{
        plus.webview.show(href, "fade-in", 300);
        this.classList.add("mui-active");
        this.classList.add("has-show");
    }
    var before = document.querySelector(".mui-active");
    plus.webview.hide(before.getAttribute("href"));
    before.classList.remove("mui-active");
}

function addHtml(element, position, templateId, data, callback)
{
    var html = template(templateId, data);
    element.insertAdjacentHTML(position, html);
    if(callback){
        callback(data);
    }
}

function requestUrl(method, url, callback, data)
{
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
            callback(JSON.parse(xhr.responseText));
        }
    };
    xhr.open(method, url);
    if(data){
        xhr.send(data);
    }
    else{
        xhr.send();
    }
}

function loadJsSort(list, index, callback)
{
    var src = list[index];
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = src;
    document.getElementsByTagName("body")[0].appendChild(script);
    script.onload = function(){
        var nextIndex = index + 1;
        if(nextIndex == list.length){
            callback();
        }
        else{
            loadJsSort(list, nextIndex, callback);
        }
    };
}

function openWindow(url)
{
    mui.openWindow({
        url: url,
        show: {
            autoShow: false
        }
    });
}
